﻿using System;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEditor;
using Neatly.UI;
using Pandora;
using PandoraEditor;
using Object = UnityEngine.Object;

namespace NeatlyEditor.UI
{
    public class UIAssetMenu
    {
        [MenuItem("Assets/UI工具/转成UseTexturePacker", false)]
        static void ImgToTexturePacker()
        {
            GameObject source = Selection.activeGameObject;
            GameObject newGo = PrefabUtility.InstantiatePrefab(source) as GameObject;
            if (UIUtility.ImageToUseTexturePacker(newGo))
            {
                PrefabUtility.ReplacePrefab(newGo, source, ReplacePrefabOptions.ConnectToPrefab);
            }

            Object.DestroyImmediate(newGo);
        }

        [MenuItem("Assets/UI工具/转成UseTexturePacker", true)]
        static bool CheckImgToTexturePacker()
        {
            return Selection.activeGameObject;
        }

        [MenuItem("Assets/UI工具/修复SpriteName", false)]
        static void FixSpriteMiss()
        {
            GameObject source = Selection.activeGameObject;
            GameObject newGo = PrefabUtility.InstantiatePrefab(source) as GameObject;
            if (UIUtility.FixSpriteMiss(newGo))
            {
                PrefabUtility.ReplacePrefab(newGo, source, ReplacePrefabOptions.ConnectToPrefab);
            }

            Object.DestroyImmediate(newGo);
        }


        [MenuItem("Assets/UI工具/修复SpriteName", true)]
        static bool CheckFixSpriteMiss()
        {
            return Selection.activeGameObject;
        }

        [MenuItem("Assets/UI工具/设置为Sprite", false)]
        static void SetUISprite()
        {
            PdrExecuteHelper.ExecuteAssetOperate(SetToUISprite, SelectionMode.Assets);
        }

        [MenuItem("Assets/UI工具/设置为Sprite", true)]
        static bool CheckSetUISprite()
        {
            return CheckIsDictionary();
        }

        [MenuItem("Assets/UI工具/格式化图片名", false)]
        static void FormatTextureName()
        {
            PdrExecuteHelper.ExecuteAssetOperate(SetFormatTextureName, SelectionMode.Assets);
        }

        [MenuItem("Assets/UI工具/格式化图片名", true)]
        static bool CheckFormatTextureName()
        {
            return CheckIsDictionary();
        }

        [MenuItem("Assets/UI工具/统一图片尺寸", false)]
        static void AddSameTrim()
        {
            PdrExecuteHelper.ExecuteAssetOperate(SameTrim.AddSameTrim, SelectionMode.Assets);
        }

        [MenuItem("Assets/UI工具/统一图片尺寸", true)]
        static bool CheckAddSameTrim()
        {
            return CheckIsDictionary();
        }

        static void SetToUISprite(Object obj)
        {
            Debug.Log(Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(obj)));
            TextureUtility.ImportUISprite(AssetDatabase.GetAssetPath(obj));
        }

        static void SetFormatTextureName(Object obj)
        {
            string rootPath = AssetDatabase.GetAssetPath(obj);
            string fileHead = rootPath.GetFileName() + "_";
            var list = PdrFileUtil.GetFilesName(rootPath);
            for (int i = 0; i < list.Count; i++)
            {
                string fileName = list[i].GetFileName();
                if (fileName.IndexOf(fileHead, StringComparison.Ordinal) != 0)
                {
                    fileName = fileHead + fileName;
                    File.Move(list[i], Path.Combine(rootPath, fileName));
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        static bool CheckIsDictionary()
        {
            Object[] items = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);
            string headPath = UserSetting.UISetting.path_atlas + "/";
            return items.Length > 0 && items.Select(AssetDatabase.GetAssetPath)
                .All(path => path.ToLower().StartsWith(headPath) && Directory.Exists(path));
        }
    }
}